package src;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

class SimplexTableau extends JPanel{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JLabel[] labelOfCoefficient_C;//价值系数C
    JLabel[][] labelOfCoefficient_A_And_X;//技术系数A和X
    JLabel[] labelOfCoefficient_B;//限额系数B
    JLabel[] labelOfTheta;//θ
    JLabel[] labelOfTitleX;//X表头
    JLabel[] labelOfBasicVariable;//基变量
    JLabel[] labelOfCheckNumber;//检验数σ
    JLabel[] labelOfCB;//CB
    JLabel labelOfTitleCj;//Cj表头
    JLabel labelOfTitleTheta;//θ表头
    JLabel labelOfTitleCB;//CB表头
    JLabel labelOfTitleXB;//XB表头
    JLabel labelOfTitleb;//b表头
    JLabel labelOfTitleCheckNumber;//检验数σ表头
    JLabel labelOfLJYNB;//LJYNB
    JLabel labelOfOptimalValue = new JLabel("", JLabel.LEFT);//最优值
    JLabel labelOfOptimalSoluttions = new JLabel("", JLabel.LEFT);//最优解
    JLabel labelOfStatus = new JLabel("迭代中...", JLabel.LEFT);//状态(迭代中or有解？)
    public  final int TABLEWIDTH = 100;//表格宽度
    public  final int TABLEHEIGHT = 100;//表格高度
    public  final int BORDERWIDTH = 2;//边框宽度
    public  int NUMBEROFCOLUMNS;//矩阵变量数
    public  int NUMBEROFLINES;//矩阵行数


    public SimplexTableau(int positionOfBasicVariable[], CommonFraction[][] a, CommonFraction[] b, CommonFraction[] c, CommonFraction[] theta, CommonFraction[] checkNumber) {
        initial(positionOfBasicVariable, a, b, c, theta, checkNumber);
    }

    //初始化表格
    private void initial(int positionOfBasicVariable[], CommonFraction[][] a, CommonFraction[] b, CommonFraction[] c, CommonFraction[] theta, CommonFraction[] checkNumber) {

        Font typeface = new Font("黑体", Font.PLAIN,30);
        NUMBEROFLINES = a.length;
        NUMBEROFCOLUMNS = a[0].length;
        this.setLayout(null);

        Border cellBoundary = BorderFactory.createLineBorder(Color.gray,BORDERWIDTH);

        labelOfStatus.setBounds((TABLEWIDTH-BORDERWIDTH)*(NUMBEROFCOLUMNS)/3, (TABLEHEIGHT-BORDERWIDTH)*(3+NUMBEROFLINES), TABLEWIDTH*8, TABLEHEIGHT);
        labelOfStatus.setFont(typeface);
        labelOfStatus.setForeground(Color.RED);
        this.add(labelOfStatus);

        labelOfOptimalSoluttions.setBounds((TABLEWIDTH-BORDERWIDTH)*(NUMBEROFCOLUMNS)/3, (TABLEHEIGHT-BORDERWIDTH)*(4+NUMBEROFLINES), TABLEWIDTH*8, TABLEHEIGHT);
        labelOfOptimalSoluttions.setFont(typeface);
        labelOfOptimalSoluttions.setForeground(Color.RED);
        this.add(labelOfOptimalSoluttions);

        labelOfOptimalValue.setBounds((TABLEWIDTH-BORDERWIDTH)*(NUMBEROFCOLUMNS)/3, (TABLEHEIGHT-BORDERWIDTH)*(5+NUMBEROFLINES), TABLEWIDTH*8, TABLEHEIGHT);
        labelOfOptimalValue.setFont(typeface);
        labelOfOptimalValue.setForeground(Color.RED);
        this.add(labelOfOptimalValue);

        labelOfTitleCj = new JLabel("Cj",JLabel.CENTER);
        labelOfTitleCj.setBounds(0, 0, (TABLEWIDTH-BORDERWIDTH)*3+BORDERWIDTH, TABLEHEIGHT);
        labelOfTitleCj.setBorder(cellBoundary);
        this.add(labelOfTitleCj);

        labelOfTitleCB = new JLabel("CB",JLabel.CENTER);
        labelOfTitleCB.setBounds(0, TABLEHEIGHT-BORDERWIDTH, TABLEWIDTH, TABLEHEIGHT);
        labelOfTitleCB.setBorder(cellBoundary);
        this.add(labelOfTitleCB);

        labelOfTitleXB = new JLabel("XB",JLabel.CENTER);
        labelOfTitleXB.setBounds(TABLEWIDTH-BORDERWIDTH, TABLEHEIGHT-BORDERWIDTH, TABLEWIDTH, TABLEHEIGHT);
        labelOfTitleXB.setBorder(cellBoundary);
        this.add(labelOfTitleXB);

        labelOfTitleb = new JLabel("b",JLabel.CENTER);
        labelOfTitleb.setBounds((TABLEWIDTH-BORDERWIDTH)*2, TABLEHEIGHT-BORDERWIDTH, TABLEWIDTH, TABLEHEIGHT);
        labelOfTitleb.setBorder(cellBoundary);
        this.add(labelOfTitleb);

        labelOfTitleTheta = new JLabel("θ",JLabel.CENTER);
        labelOfTitleTheta.setBounds((TABLEWIDTH-BORDERWIDTH)*(3+NUMBEROFCOLUMNS), 0, TABLEWIDTH, (TABLEHEIGHT-BORDERWIDTH)*2+BORDERWIDTH);
        labelOfTitleTheta.setBorder(cellBoundary);
        this.add(labelOfTitleTheta);

        labelOfTitleCheckNumber = new JLabel("σj = cj - zj",JLabel.CENTER);
        labelOfTitleCheckNumber.setBounds(0, (TABLEHEIGHT-BORDERWIDTH)*(2+NUMBEROFLINES), (TABLEWIDTH-BORDERWIDTH)*3+BORDERWIDTH, TABLEHEIGHT);
        labelOfTitleCheckNumber.setBorder(cellBoundary);
        this.add(labelOfTitleCheckNumber);

//		labelOfLJYNB = new JLabel("LJYNB",JLabel.CENTER);
        labelOfLJYNB = new JLabel("大爱王莉老师",JLabel.CENTER);
        labelOfLJYNB.setForeground(Color.RED);
        labelOfLJYNB.setBounds((TABLEWIDTH-BORDERWIDTH)*(3+NUMBEROFCOLUMNS), (TABLEHEIGHT-BORDERWIDTH)*(2+NUMBEROFLINES), TABLEWIDTH, TABLEHEIGHT);
        labelOfLJYNB.setBorder(cellBoundary);
        this.add(labelOfLJYNB);



        for(int i=0; i<NUMBEROFLINES; i++) {

            labelOfCB = new JLabel[TABLEHEIGHT];
            labelOfCB[i] = new JLabel(c[positionOfBasicVariable[i]].getCommonFraction(),JLabel.CENTER);
            labelOfCB[i].setBounds(0, (TABLEHEIGHT-BORDERWIDTH)*(2+i), TABLEWIDTH, TABLEHEIGHT);
            labelOfCB[i].setBorder(cellBoundary);
            this.add(labelOfCB[i]);

            labelOfBasicVariable = new JLabel[TABLEHEIGHT];
            labelOfBasicVariable[i] = new JLabel("X"+(positionOfBasicVariable[i]+1),JLabel.CENTER);
            labelOfBasicVariable[i].setBounds(TABLEWIDTH-BORDERWIDTH, (TABLEHEIGHT-BORDERWIDTH)*(2+i), TABLEWIDTH, TABLEHEIGHT);
            labelOfBasicVariable[i].setBorder(cellBoundary);
            this.add(labelOfBasicVariable[i]);

            labelOfCoefficient_B = new JLabel[TABLEHEIGHT];
            labelOfCoefficient_B[i] = new JLabel(b[i].getCommonFraction(),JLabel.CENTER);
            labelOfCoefficient_B[i].setBounds((TABLEWIDTH-BORDERWIDTH)*2, (TABLEHEIGHT-BORDERWIDTH)*(2+i), TABLEWIDTH, TABLEHEIGHT);
            labelOfCoefficient_B[i].setBorder(cellBoundary);
            this.add(labelOfCoefficient_B[i]);

            labelOfTheta = new JLabel[TABLEHEIGHT];
            if(theta[i] == null) {
                labelOfTheta[i] = new JLabel("——",JLabel.CENTER);
            }
            else {
                labelOfTheta[i] = new JLabel(theta[i].getCommonFraction(),JLabel.CENTER);
            }
            labelOfTheta[i].setBounds((TABLEWIDTH-BORDERWIDTH)*(3+NUMBEROFCOLUMNS), (TABLEHEIGHT-BORDERWIDTH)*(2+i), TABLEWIDTH, TABLEHEIGHT);
            labelOfTheta[i].setBorder(cellBoundary);
            this.add(labelOfTheta[i]);

            labelOfCoefficient_A_And_X = new JLabel[TABLEHEIGHT][TABLEWIDTH];
            for(int j=0; j<NUMBEROFCOLUMNS; j++) {
                labelOfCoefficient_A_And_X[i][j] = new JLabel(a[i][j].getCommonFraction(),JLabel.CENTER);
                labelOfCoefficient_A_And_X[i][j].setBounds((TABLEWIDTH-BORDERWIDTH)*(3+j), (TABLEHEIGHT-BORDERWIDTH)*(2+i), TABLEWIDTH, TABLEHEIGHT);
                labelOfCoefficient_A_And_X[i][j].setBorder(cellBoundary);
                this.add(labelOfCoefficient_A_And_X[i][j]);
            }
        }


        labelOfCoefficient_C = new JLabel[TABLEWIDTH];
        labelOfTitleX = new JLabel[TABLEWIDTH];
        labelOfCheckNumber = new JLabel[TABLEWIDTH];
        for(int j=0; j<NUMBEROFCOLUMNS; j++) {
            labelOfCoefficient_C[j] = new JLabel(c[j].getCommonFraction(), JLabel.CENTER);
            labelOfCoefficient_C[j].setBounds((TABLEWIDTH-BORDERWIDTH)*(3+j), 0, TABLEWIDTH, TABLEHEIGHT);
            labelOfCoefficient_C[j].setBorder(cellBoundary);
            this.add(labelOfCoefficient_C[j]);

            labelOfTitleX[j] = new JLabel("X"+(j+1), JLabel.CENTER);
            labelOfTitleX[j].setBounds((TABLEWIDTH-BORDERWIDTH)*(3+j), TABLEHEIGHT-BORDERWIDTH, TABLEWIDTH, TABLEHEIGHT);
            labelOfTitleX[j].setBorder(cellBoundary);
            this.add(labelOfTitleX[j],JLabel.CENTER);

            labelOfCheckNumber[j] = new JLabel(checkNumber[j].getCommonFraction(), JLabel.CENTER);
            labelOfCheckNumber[j].setBounds((TABLEWIDTH-BORDERWIDTH)*(3+j), (TABLEHEIGHT-BORDERWIDTH)*(2+NUMBEROFLINES), TABLEWIDTH, TABLEHEIGHT);
            labelOfCheckNumber[j].setBorder(cellBoundary);
            this.add(labelOfCheckNumber[j]);

        }

        this.setSize((TABLEWIDTH-BORDERWIDTH)*(4+NUMBEROFCOLUMNS)+BORDERWIDTH, (TABLEHEIGHT-BORDERWIDTH)*(3+NUMBEROFLINES)+BORDERWIDTH);

    }
}